package com.android.common;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.Toast;

public class Action {

	private Context ctx;
	LoadingDailog dialog;

	public Action(Context ctxs) {
		this.ctx = ctxs;
	}

	static double DEF_PI = 3.14159265359; // PI
	static double DEF_2PI = 6.28318530712; // 2*PI
	static double DEF_PI180 = 0.01745329252; // PI/180.0
	static double DEF_R = 6370693.5; // radius of earth

	public static int GetShortDistance(double lon1, double lat1, double lon2,
			double lat2) {
		double ew1, ns1, ew2, ns2;
		double dx, dy, dew;
		double distance;
		// 角度转换为弧度
		ew1 = lon1 * DEF_PI180;
		ns1 = lat1 * DEF_PI180;
		ew2 = lon2 * DEF_PI180;
		ns2 = lat2 * DEF_PI180;
		// 经度差
		dew = ew1 - ew2;
		// 若跨东经和西经180 度，进行调整
		if (dew > DEF_PI)
			dew = DEF_2PI - dew;
		else if (dew < -DEF_PI)
			dew = DEF_2PI + dew;
		dx = DEF_R * Math.cos(ns1) * dew; // 东西方向长度(在纬度圈上的投影长度)
		dy = DEF_R * (ns1 - ns2); // 南北方向长度(在经度圈上的投影长度)
		// 勾股定理求斜边长
		distance = Math.sqrt(dx * dx + dy * dy);
		return (int) distance;
	}

	// // 注销
	// public void logOut() {
	// SharedPreferences sp = ctx
	// .getSharedPreferences(Common.Userbase_Info, 0);
	// Editor spEd = sp.edit();
	// spEd.putBoolean("isLogin", false);
	// spEd.putString("UserName", "");
	// spEd.putString("Pass", "");
	// spEd.commit();
	// Common.userid = "";
	// Common.userName = "";
	// Common.model = null;
	// }

	// 弹出是否退出应用对话框
	public void exit(final Activity mCtx) {
		AlertDialog.Builder builder = new Builder(mCtx);
		builder.setTitle("提示")
				.setMessage("确定退出？")
				.setPositiveButton("确定",
						new android.content.DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {
								mCtx.finish();
							}
	}).setNegativeButton("取消", null).show();

	}

	// 启动进度框
	public void startProgressDialog(String dialogMsg) {

		if (ctx != null) {
			if (dialog != null && dialog.isShowing()) {
				dialog.UpMsg(dialogMsg);
			} else {
				dialog = new LoadingDailog(ctx, dialogMsg);
			}
			dialog.show();
		}
	}

	// 停止进度框
	public void stopProgressDialog() {
		if (ctx != null) {
			if (dialog != null) {
				if (dialog.isShowing()) {
					dialog.cancel();
				}
				dialog = null;
			}
		}
		ctx = null;
	}

	public void showLongToast(String toastMsg) {
		Toast.makeText(ctx, toastMsg, Toast.LENGTH_SHORT).show();
	}

	public void showShortToast(String toastMsg) {
		Toast.makeText(ctx, toastMsg, Toast.LENGTH_SHORT).show();
	}

}
